<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of GoodsController
 *
 * @author dehua310
 */
class CompanyUserController extends AdminController {

    public function index() {
        $tab = I('tab', 1, 'intval');

        if ($tab == 1) {
            $where['c.status'] = 1;
        } elseif ($tab == 2) {
            $where['c.status'] = 2;
            $where['cu.is_root'] = 1;
        } else {
            $where['c.status'] = 0;
            $where['cu.is_root'] = 1;
        }

        $phone = I('phone');
        $company_name = I('company_name');
        if ($phone) {
            $where['cu.company_user_phone'] = array('like', "%{$phone}%");
            $this->assign('phone', $phone);
        }
        if ($company_name) {
            $where['c.company_name'] = array('like', "%{$company_name}%");
            $this->assign('company_name', $company_name);
        }
        $M = M()->table("ot_company_user as cu")
                ->join('ot_company AS c on cu.company_id = c.company_id');
        $field = 'cu.status as user_status,cu.is_root,c.total_amount,cu.company_user_phone,cu.company_user_id,c.company_id,c.company_name,c.linkman,c.company_phone,c.qq,c.address,c.company_licence_img,c.status,c.refund_desc';
        $order = "cu.company_user_id DESC";
        $list = $this->lists($M, $where, $order, $field);

        $this->assign('tab', $tab);
        $this->assign('_list', $list);
        $this->display();
    }

    public function edit() {
        $company_id = I('company_id', 0, 'intval');
        $company_user_id = I('company_user_id', 0, 'intval');
        $res = 0;
        if ($company_id) {
            $company_status = I('company_status', 0, 'intval');
            $data['status'] = $company_status;
            if ($company_status == 2) {
                $data['refund_desc'] = I('refund_desc');
            }
            $res = M('company')->where(array('company_id' => $company_id))->save($data);
        }
        if ($company_user_id) {
            $status = I('status', 0, 'intval');
            $res = M('CompanyUser')->where(array('company_user_id' => $company_user_id))->save(array('status' => $status));
        }

        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    public function chongzhi() {
        $company_id = I('company_id', 0, 'intval');
        $total_amount = I('total_amount', 0);

        $res = M('company')->where(array('company_id' => $company_id))->setInc('total_amount', $total_amount);
        
        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

}
